//
//  SDDownloadRequestOperation.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDDownloadRequestOperation.h"
@interface SDDownloadRequestOperation ()
@property (nonatomic,strong,readwrite)  NSString*   cachePath;
@end

@implementation SDDownloadRequestOperation
@synthesize outputStream = _outputStream;
- (NSOutputStream *)outputStream {
    if (!_outputStream) {
        if( self.cachePath == nil )
            self.cachePath = [NSString stringWithFormat:@"%@/%@",NSTemporaryDirectory(),[[self.request.URL absoluteString] MD5EncodedString]];
        self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.cachePath append:NO];
    }
    
    return _outputStream;
}

- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                              failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    // completionBlock is manually nilled out in AFURLConnectionOperation to break the retain cycle.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
#pragma clang diagnostic ignored "-Wgnu"
    self.completionBlock = ^{
        if (self.error) {
            if (failure) {
                dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
                    failure(self, self.error);
                });
            }
        } else {
            if (success) {
                dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
                    success(self, self.cachePath);
                });
            }
        }
    };
#pragma clang diagnostic pop
}
@end

